package router

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"go-task/common"
	"go-task/handler"
)

func NewRouter() *gin.Engine {
	gin.SetMode(gin.ReleaseMode)
	r := gin.Default()

	corsConfig := cors.DefaultConfig()
	corsConfig.AllowAllOrigins = true

	r.Use(cors.New(corsConfig))
	r.Use(AuthHandler())
	r.Use(GlobalExceptionHandler)

	// 配置路由
	r.NoRoute(func(c *gin.Context) {
		common.Fail(c, common.ErrPath)
	})
	authGroup := r.Group("/auth")
	{
		authGroup.POST("/login", handler.Login)
		authGroup.POST("/logout", handler.Logout)
	}

	taskGroup := r.Group("/task")
	{
		taskGroup.GET("/test", handler.Test)
		taskGroup.POST("/addTask", handler.AddTask)
		taskGroup.GET("/taskList", handler.TaskList)
		taskGroup.POST("/updateTaskStatus", handler.UpdateTaskStatus)
		taskGroup.POST("/deleteTask", handler.DeleteTask)
		taskGroup.POST("/execTaskTest", handler.ExecTaskTest)
	}

	return r
}
